package com.ler.yrmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.product.entity.AttrAttrGroupRelationEntity;
import com.ler.yrmall.product.entity.AttrEntity;
import com.ler.yrmall.product.entity.AttrGroupEntity;
import com.ler.yrmall.product.form.AttrGroupForm;
import com.ler.yrmall.product.mapper.AttrGroupMapper;
import com.ler.yrmall.product.service.AttrAttrgroupRelationService;
import com.ler.yrmall.product.service.AttrGroupService;
import com.ler.yrmall.product.service.AttrService;
import com.ler.yrmall.product.vo.AttrGroupVO;
import com.ler.yrmall.product.vo.SpuBaseAttrGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @author leron
 */
@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public IPage<AttrGroupVO> find(AttrGroupForm request) {
        return this.baseMapper.find(request.getPage(),request);
    }

    @Override
    public List<AttrGroupVO> list(AttrGroupForm request) {
        return this.baseMapper.list(request);
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrGroupId) {
        List<AttrAttrGroupRelationEntity> list = attrAttrgroupRelationService
                .list(Wrappers.<AttrAttrGroupRelationEntity>lambdaQuery()
                        .eq(AttrAttrGroupRelationEntity::getAttrGroupId, attrGroupId));
        if (list.size() == 0) {
            return Collections.emptyList();
        }
        List<Long> collect = list.stream().map(AttrAttrGroupRelationEntity::getAttrId).collect(Collectors.toList());
        return attrService.listByIds(collect);
    }

    @Override
    public IPage<AttrEntity> getNoAttrRelation(AttrGroupForm request) {
        AttrGroupEntity entity = this.getById(request.getId());

        List<AttrGroupEntity> entityList = this.list(Wrappers.<AttrGroupEntity>lambdaQuery()
                .eq(AttrGroupEntity::getCategoryId, entity.getCategoryId()));

        List<Long> ids = entityList.stream().map(AttrGroupEntity::getId).collect(Collectors.toList());

        List<AttrAttrGroupRelationEntity> relationList = attrAttrgroupRelationService
                .list(Wrappers.<AttrAttrGroupRelationEntity>lambdaQuery().in(AttrAttrGroupRelationEntity::getAttrGroupId, ids));

        List<Long> rids = relationList.stream().map(AttrAttrGroupRelationEntity::getAttrId).collect(Collectors.toList());
        LambdaQueryWrapper<AttrEntity> wrapper = Wrappers.<AttrEntity>lambdaQuery().eq(AttrEntity::getCategoryId, entity.getCategoryId());
        if (rids.size() > 0) {
            wrapper.notIn(AttrEntity::getId, rids)
                    .eq(AttrEntity::getAttrType, AttrEntity.AttrType.BASIC.getCode());
        }
        return attrService.page(request.getPage(), wrapper);
    }

    @Override
    public List<AttrGroupVO> getAttrGroupWithAttrs(Long categoryId) {
        // 分组
        List<AttrGroupEntity> groups = this.list(Wrappers.<AttrGroupEntity>lambdaQuery().eq(AttrGroupEntity::getCategoryId, categoryId));

        List<Long> gIds = groups.stream().map(AttrGroupEntity::getId).collect(Collectors.toList());

        Map<Long, List<AttrAttrGroupRelationEntity>> bindMap = attrAttrgroupRelationService.list(Wrappers.<AttrAttrGroupRelationEntity>lambdaQuery()
                .in(AttrAttrGroupRelationEntity::getAttrGroupId, gIds)).stream().collect(Collectors.groupingBy(AttrAttrGroupRelationEntity::getAttrGroupId));

        return groups.stream().map(item -> {
            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(item, attrGroupVO);

            List<Long> attrIds = Optional.ofNullable(bindMap.get(item.getId()))
                    .map(List::stream).orElse(Stream.empty())
                    .map(AttrAttrGroupRelationEntity::getAttrId).collect(Collectors.toList());

            if (attrIds.size() == 0) {
                return attrGroupVO;
            }
            List<AttrEntity> attrEntities = attrService.listByIds(attrIds);
            attrGroupVO.setAttrs(attrEntities);
            return attrGroupVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuBaseAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        return this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }
}